package com.example.myapplication.module1.activity

import android.view.View
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityCustomerManagerListBinding
import com.example.myapplication.entity.AppointmentVisitBean
import com.example.myapplication.module1.adapter.CustomerManagerListAdapter
import com.example.myapplication.module1.presenter.CustomerManagerListPresenter
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.myapplication.soaprequest.request.CustomerManagerListRequest
import com.example.myapplication.soaprequest.request.SoapRequestBody
import com.example.myapplication.widget.CustomerToolbar
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.GsonUtil
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.impl.LoadingPopupView

class CustomerManagerListActivity :
    BaseMvpActivity<CustomerManagerListActivity, CustomerManagerListPresenter, ActivityCustomerManagerListBinding>() {
    private var customerManagerListRequest: CustomerManagerListRequest? = null
    private val soapRequestBody = SoapRequestBody()
    private val soapEnvelop = SoapEnvelop()
    lateinit var loadPopup: LoadingPopupView

    private val tempCustomerListBeanList = mutableListOf<AppointmentVisitBean>()
    override fun getPresenter(): CustomerManagerListPresenter {
        return CustomerManagerListPresenter()
    }

    override fun getBinding(): ActivityCustomerManagerListBinding {
        return ActivityCustomerManagerListBinding.inflate(layoutInflater)
    }

    override fun initView() {
        loadPopup = builder.asLoading(getString(R.string.loading))
        CustomerManagerListAdapter(this)

        viewBinding.list.rvList.adapter
        viewBinding.clToolbar.clickListener = object : CustomerToolbar.ClickListener {
            override fun onBackListener() {
                finish()
            }

            override fun onRightListener() {
                setResult(RESULT_OK)
                finish()
            }
        }
    }

    override fun initData() {
        customerManagerListRequest =
            bundleExtra?.getParcelable<CustomerManagerListRequest>("customerManagerListRequest")

        getCustomerList()
    }

    fun getCustomerList() {
        customerManagerListRequest?.let { soapRequestBody.value = it }
        soapEnvelop.body = soapRequestBody
        mPresenter?.getCustomerList(soapEnvelop)
    }

    fun showLoad() {
        if (!viewBinding.list.srlRefresh.isRefreshing && !viewBinding.list.srlRefresh.isLoading) {
            loadPopup.show()
        }
    }

    fun dismissLoad() {
        loadPopup.dismiss()
        if (viewBinding.list.srlRefresh.isRefreshing || viewBinding.list.srlRefresh.isLoading) {
            viewBinding.list.srlRefresh.finishRefresh()
            viewBinding.list.srlRefresh.finishLoadMore()
        }
    }

    fun setCustomerList(result: String) {
        val customerListBeanList = GsonUtil.fromJson<MutableList<AppointmentVisitBean>>(
            result,
            object : TypeToken<MutableList<AppointmentVisitBean>>() {}.type
        )
        if (customerListBeanList.isNullOrEmpty()) {
            viewBinding.list.srlRefresh.setEnableLoadMore(false)
        } else {
            viewBinding.list.srlRefresh.setEnableLoadMore(true)
            tempCustomerListBeanList.addAll(customerListBeanList)
        }


        if (tempCustomerListBeanList.isEmpty()) {
            viewBinding.noData.clNoData.visibility = View.VISIBLE
        }else{
            viewBinding.noData.clNoData.visibility = View.GONE
        }

    }
}
